#!/bin/bash

SSH_SERVER=deploy@10.10.194.112
SSH_PORT=10022

#用户列表
BASE_DIR=$(cd `dirname $0`;pwd)
MAIL_CONFIG_INI=$BASE_DIR/mailconfig.ini
USERS=$(cat $MAIL_CONFIG_INI)

CHECKPOINT=$BASE_DIR/.checkpoint
RST_TOPIC="服务宕机重启-$(hostname -I | awk '{print $1}')"
ERR_TOPIC="服务宕机警告-$(hostname -I | awk '{print $1}')"
FIX_TOPIC="服务宕机修复-$(hostname -I | awk '{print $1}')"
COUNT=$(docker ps --format "table {{.ID}}\t{{.Status}}\t{{.Names}}"|grep -E 'unhealthy|starting'|awk '{print $NF}'|wc -l)
if [ $COUNT -gt 0 ]
then
  # 状态告警
  docker ps --format "table {{.ID}}\t{{.Status}}\t{{.Names}}" > $CHECKPOINT
	cat $CHECKPOINT|ssh -p $SSH_PORT $SSH_SERVER "mailx -s $ERR_TOPIC $USERS"

  # 重启应用并通知
	RESTART_APPS=$(cat $CHECKPOINT|grep -E 'minutes|hours'|grep unhealthy|awk '{print $NF}')
	if [ ${#RESTART_APPS[@]} -gt 0 ]
	then
    echo "以下应用将自动重启:"&& echo $RESTART_APPS|ssh -p $SSH_PORT $SSH_SERVER "mailx -s $RST_TOPIC $USERS"
    for APP in ${RESTART_APPS[@]}
    do
      docker restart $APP &
    done
  fi

else
    LINES=$(cat $CHECKPOINT|wc -l)
    if [ $LINES -gt 0 ]
    then
        docker ps --format "table {{.ID}}\t{{.Status}}\t{{.Names}}"|ssh -p $SSH_PORT $SSH_SERVER "mailx -s $FIX_TOPIC $USERS"
        > $CHECKPOINT
    fi
fi;
